<template>
  <div>
    <div class="top-box">
      <el-button type="success" @click="add">添加专题</el-button>
    </div>
    <loading v-if="loading"></loading>
    <div class="pd" v-else>
      <div class="search-box">
        <el-input v-model="searchParam.id" placeholder="请输入编号" class="user-input"></el-input>
        <el-input v-model="searchParam.name" placeholder="请输入专题名称" class="user-input"></el-input>
        <el-select v-model="searchParam.cate_id" placeholder="请选择分类" class="user-input">
          <el-option v-for="item in categoryOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
        <el-button type="primary" class="el-icon-search" @click="search">搜索</el-button>
        <el-button class="el-icon-q" @click="cancel">撤销</el-button>
      </div>

      <el-table :data="listData.list" border fit highlight-current-row style="width:100%">
        <el-table-column label="编号" prop="id"></el-table-column>
        <el-table-column label="专题名称" prop="name"></el-table-column>
        <el-table-column label="顶部标题" prop="top_title"></el-table-column>
        <el-table-column label="分类" prop="cate_name"></el-table-column>
        <el-table-column label="专题banner">
          <template scope="scope">
            <img :src="scope.row.banner" class="art-cover" @click="previewImg(scope.row.banner)">
          </template>
        </el-table-column>
        <el-table-column label="站点推荐页缩略图">
          <template scope="scope">
            <img :src="scope.row.thumbnail" class="art-cover" @click="previewImg(scope.row.thumbnail)">
          </template>
        </el-table-column>
        <el-table-column label="专题背景介绍缩略图">
          <template scope="scope">
            <img :src="scope.row.bg_thumbnail" class="art-cover" @click="previewImg(scope.row.bg_thumbnail)">
          </template>
        </el-table-column>
        <!--<el-table-column label="专题简介" prop="note"></el-table-column>
        <el-table-column label="专题结语" prop="subject_desc"></el-table-column>-->
        <el-table-column label="发布时间" prop="repub_time"></el-table-column>
        <el-table-column label="添加时间" prop="add_time"></el-table-column>
        <el-table-column label="是否显示菜单">
          <template scope="scope">
            <div class="input-group-box">
              <el-switch
                v-model="scope.row.show_nav"
                on-text="是"
                off-text="否"
                @change="changeShowNav($event, scope.row.id, scope.row.title, scope.$index)">
              </el-switch>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <i class="i-icon icon-revamp" title="修改"  @click="edit(scope.row)"></i>
            <i class="i-icon icon-delete" title="删除"  @click="remove(scope.row.id)"></i>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @current-change="handleChange"
          :current-page.sync="pageall"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="listData.total">
        </el-pagination>
      </div>
      <el-dialog
        title="查看封面图"
        :visible.sync="imgVisible"
        size="tiny">
        <img :src="imgUrl" style="width:100%">
      </el-dialog>
    </div>
    <el-dialog title="添加专题" :visible.sync="showAddModelFlag" size="small">
      <div class="input-group">
        <label><i>*</i>专题名称：</label>
        <div class="input-group-box"><el-input v-model="addParam.name" placeholder="请输入专题名称"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>分类：</label>
        <div class="input-group-box">
          <el-select v-model="addParam.cate_id" placeholder="请选择分类" class="user-input">
            <el-option v-for="item in categoryOptions"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>banner图片：</label>
        <div class="input-group-box cover-box">
          <div class="el-upload el-upload--picture-card">
            <i class="el-icon-plus"></i>
            <input type="file" id="cover-img" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" class="zi_upload_input" @change="getfile($event)">
          </div>
          <img :src="addParam.banner.url" class="cover" v-if="addParam.banner.url!==''">
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>站点首页推荐缩略图：</label>
        <div class="input-group-box cover-box">
          <div class="el-upload el-upload--picture-card">
            <i class="el-icon-plus"></i>
            <input type="file" id="thumbnail" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" class="zi_upload_input" @change="getThumbnailFile($event)">
          </div>
          <img :src="addParam.thumbnail.url" class="cover" v-if="addParam.thumbnail.url!==''">
          <span class="p-tip">* 建议尺寸580*400</span>
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>专题背景介绍缩略图：</label>
        <div class="input-group-box cover-box">
          <div class="el-upload el-upload--picture-card">
            <i class="el-icon-plus"></i>
            <input type="file" id="bg_thumbnail2" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" class="zi_upload_input" @change="getBgThumbnailFile($event)">
          </div>
          <img :src="addParam.bgThumbnail.url" class="cover" v-if="addParam.bgThumbnail.url!==''">
          <span class="p-tip">* 建议尺寸565*180</span>
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>顶部标题：</label>
        <div class="input-group-box"><el-input v-model="addParam.top_title" placeholder="请输入顶部标题名称"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>专题简介：</label>
        <div class="input-group-box">
          <el-input v-model="addParam.note" type="textarea" :rows="5" placeholder="请输入专题简介"></el-input>
        </div>
      </div>
	    <div class="input-group">
        <label>专题结语：</label>
        <div class="input-group-box">
          <el-input v-model="addParam.subject_desc" type="textarea" :rows="5" placeholder="请输入专题结语"></el-input>
        </div>
      </div>
      <div class="input-group">
        <label>发布时间：</label>
        <el-date-picker
          v-model="addParam.repub_time"
          type="datetime"
          placeholder="请选择发布时间"
          format="yyyy-MM-dd HH:mm:ss"
          default-time="12:00:00">
        </el-date-picker>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改专题" :visible.sync="showUpdateModelFlag" size="small">
      <div class="input-group">
        <label><i>*</i>专题名称：</label>
        <div class="input-group-box"><el-input v-model="editParam.name" placeholder="请输入专题名称"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>分类：</label>
        <div class="input-group-box">
          <el-select v-model="editParam.cate_id" placeholder="请选择分类" class="user-input">
            <el-option v-for="item in categoryOptions"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>专题banner：</label>
        <div class="input-group-box cover-box">
          <div class="el-upload el-upload--picture-card">
            <i class="el-icon-plus"></i>
            <input type="file" id="cover-img2" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" class="zi_upload_input" @change="editfile($event)">
          </div>
          <img :src="editParam.banner.url" class="cover" v-if="editParam.banner.url!==''">
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>站点首页推荐缩略图：</label>
        <div class="input-group-box cover-box">
          <div class="el-upload el-upload--picture-card">
            <i class="el-icon-plus"></i>
            <input type="file" id="thumbnail2" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" class="zi_upload_input" @change="editThumbnailFile($event)">
          </div>
          <img :src="editParam.thumbnail.url" class="cover" v-if="editParam.thumbnail.url!==''">
          <span class="p-tip">* 建议尺寸580*400</span>
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>专题背景介绍缩略图：</label>
        <div class="input-group-box cover-box">
          <div class="el-upload el-upload--picture-card">
            <i class="el-icon-plus"></i>
            <input type="file" id="bg_thumbnail" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" class="zi_upload_input" @change="editBgThumbnailFile($event)">
          </div>
          <img :src="editParam.bgThumbnail.url" class="cover" v-if="editParam.bgThumbnail.url!==''">
          <span class="p-tip">* 建议尺寸565*180</span>
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>顶部标题：</label>
        <div class="input-group-box"><el-input v-model="editParam.top_title" placeholder="请输入顶部标题名称"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>专题简介：</label>
        <div class="input-group-box">
          <el-input v-model="editParam.note" type="textarea" :rows="5" placeholder="请输入专题简介"></el-input>
        </div>
      </div>
	    <div class="input-group">
        <label><i>*</i>专题结语：</label>
        <div class="input-group-box">
          <el-input v-model="editParam.subject_desc" type="textarea" :rows="5" placeholder="请输入专题结语"></el-input>
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>发布时间：</label>
        <el-date-picker
          v-model="editParam.repub_time"
          type="datetime"
          placeholder="请选择发布时间"
          format="yyyy-MM-dd HH:mm:ss"
          default-time="12:00:00">
        </el-date-picker>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showUpdateModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import apiConfig from '../../utils/apiconfig'
import api from '../../utils/api'
import helper from '../../utils/helper'
import loading from '../../components/loading'
export default {
  data () {
    return {
      listData: [],
      pageSize: 20,
      pageall: 1,
      page: 1,
      loading: true,
      showAddModelFlag: false,
      showUpdateModelFlag: false,
      imgUrl: '',
      imgVisible: false,
      categoryOptions: [],
      searchParam: {
        id: '',
        name: '',
        cate_id: ''
      },
      addParam: {
        name: '',
        cate_id: '',
        note: '',
        subject_desc: '',
        repub_time: '',
        top_title: '',
        banner: {
          src: '',
          url: ''
        },
        thumbnail: {
          src: '',
          url: ''
        },
        bgThumbnail: {
          src: '',
          url: ''
        }
      },
      editParam: {
        id: '',
        name: '',
        cate_id: '',
        note: '',
        subject_desc: '',
        repub_time: '',
        top_title: '',
        banner: {
          src: '',
          url: ''
        },
        thumbnail: {
          src: '',
          url: ''
        },
        bgThumbnail: {
          src: '',
          url: ''
        }
      }
    }
  },
  created () {
    this.getCategoryLevelOption()
    this.getList()
  },
  components: {
    loading
  },
  methods: {
    getSearchParam () { // 参数
      var param = {
        page: this.page,
        id: this.searchParam.id,
        name: this.searchParam.name,
        cate_id: this.searchParam.cate_id
      }
      this.params = param
    },
    getList () {
      this.getSearchParam()
      api.GET(helper.getManageApiUrl(apiConfig.subjectList), this.params, res => {
        this.listData = res
        let list = this.listData.list
        list.forEach((item, index) => {
          if (item.show_nav === 0) {
            item.show_nav = false
          } else {
            item.show_nav = true
          }
        })
        this.loading = false
      }, res => {
        console.log(res)
      })
    },
    search () {
      this.getList()
    },
    cancel () {
      this.searchParam.name = ''
      this.searchParam.cate_id = ''
      this.searchParam.banner = ''
      this.searchParam.note = ''
      this.searchParam.subject_desc = ''
      this.searchParam.repub_time = ''
      this.searchParam.top_title = ''
    },
    handleChange (val) {
      this.page = val
      this.getList()
    },
    add () {
      this.showAddModelFlag = true
    },
    confirmAdd () {
      let formData = new FormData()
      formData.append('name', this.addParam.name)
      formData.append('banner', this.addParam.banner.src)
      formData.append('thumbnail', this.addParam.thumbnail.src)
      formData.append('bg_thumbnail', this.addParam.bgThumbnail.src)
      formData.append('cate_id', this.addParam.cate_id)
      formData.append('note', this.addParam.note)
      formData.append('subject_desc', this.addParam.subject_desc)
      formData.append('repub_time', helper.formatDateTime(this.addParam.repub_time))
      formData.append('top_title', this.addParam.top_title)
      let configs = {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }
      api.POST(helper.getManageApiUrl(apiConfig.addSubject), formData, configs, res => {
        this.$notify({
          title: '成功',
          message: '添加成功',
          type: 'success',
          duration: 1500
        })
        this.addParam.name = ''
        this.addParam.cate_id = ''
        this.addParam.note = ''
        this.addParam.subject_desc = ''
        this.addParam.repub_time = ''
        this.addParam.top_title = ''
        var cover = document.getElementById('cover-img')
        cover.value = ''
        this.addParam.banner.url = ''
        let thumbnail = document.getElementById('thumbnail')
        thumbnail.value = ''
        let bgThumbnail = document.getElementById('bg_thumbnail')
        bgThumbnail.value = ''
        this.addParam.thumbnail.url = ''
        this.addParam.bgThumbnail.url = ''
        this.showAddModelFlag = false
        this.getList()
      }, res => {
        this.$notify({
          title: '失败',
          message: res.msg,
          type: 'fail',
          duration: 1500
        })
      })
    },
    remove (id) {
      var param = {
        id: id
      }
      this.$confirm('确认要删除此条信息吗？')
        .then(_ => {
          api.GET(helper.getManageApiUrl(apiConfig.deleteSubject), param, res => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 1500
            })
            this.getList()
          }, res => {
            this.$notify({
              title: '失败',
              message: '删除失败',
              type: 'error',
              duration: 1500
            })
          })
        })
        .catch(_ => {})
    },
    edit (item) {
      this.editParam.id = item.id
      this.editParam.name = item.name
      this.editParam.cate_id = item.cate_id
      this.editParam.banner.src = item.banner
      this.editParam.banner.url = item.banner
      this.editParam.thumbnail.src = item.thumbnail
      this.editParam.thumbnail.url = item.thumbnail
      this.editParam.bgThumbnail.src = item.bg_thumbnail
      this.editParam.bgThumbnail.url = item.bg_thumbnail
      this.editParam.note = item.note
      this.editParam.subject_desc = item.subject_desc
      this.editParam.repub_time = item.repub_time
      this.editParam.top_title = item.top_title
      this.showUpdateModelFlag = true
    },
    save () {
      let formData = new FormData()
      formData.append('id', this.editParam.id)
      formData.append('name', this.editParam.name)
      formData.append('banner', this.editParam.banner.src)
      formData.append('thumbnail', this.editParam.thumbnail.src)
      formData.append('bg_thumbnail', this.editParam.bgThumbnail.src)
      formData.append('cate_id', this.editParam.cate_id)
      formData.append('note', this.editParam.note)
      formData.append('subject_desc', this.editParam.subject_desc)
      formData.append('top_title', this.editParam.top_title)
      formData.append('repub_time', helper.formatDateTime(this.editParam.repub_time))
      let configs = {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }
      api.POST(helper.getManageApiUrl(apiConfig.updateSubject), formData, configs, res => {
        this.$notify({
          title: '成功',
          message: '修改成功',
          type: 'success',
          duration: 1500
        })
        var cover = document.getElementById('cover-img2')
        cover.value = ''
        this.editParam.id = ''
        this.editParam.banner.url = ''
        this.editParam.thumbnail.url = ''
        this.editParam.bgThumbnail.url = ''
        this.editParam.name = ''
        this.editParam.cate_id = ''
        this.editParam.note = ''
        this.editParam.subject_desc = ''
        this.editParam.repub_time = ''
        this.editParam.top_title = ''
        this.showUpdateModelFlag = false
        this.getList()
      }, res => {
        this.$notify({
          title: '失败',
          message: '修改失败',
          type: 'error',
          duration: 1500
        })
      })
    },

    getCategoryLevelOption () {
      var param = {
        param: ''
      }
      api.GET(helper.getManageApiUrl(apiConfig.getSubjectCategoryOptions), param, res => {
        this.categoryOptions = res
      }, res => {
        console.log(res)
      })
    },
    getfile (event) {
      this.addParam.banner.src = event.target.files[0]
      this.addParam.banner.url = window.URL.createObjectURL(this.addParam.banner.src)
    },
    getThumbnailFile (event) {
      this.addParam.thumbnail.src = event.target.files[0]
      this.addParam.thumbnail.url = window.URL.createObjectURL(this.addParam.thumbnail.src)
    },
    getBgThumbnailFile (event) {
      this.addParam.bgThumbnail.src = event.target.files[0]
      this.addParam.bgThumbnail.url = window.URL.createObjectURL(this.addParam.bgThumbnail.src)
    },

    editfile (event) {
      this.editParam.banner.src = event.target.files[0]
      this.editParam.banner.url = window.URL.createObjectURL(this.editParam.banner.src)
    },
    editThumbnailFile (event) {
      this.editParam.thumbnail.src = event.target.files[0]
      this.editParam.thumbnail.url = window.URL.createObjectURL(this.editParam.thumbnail.src)
    },
    editBgThumbnailFile (event) {
      this.editParam.bgThumbnail.src = event.target.files[0]
      this.editParam.bgThumbnail.url = window.URL.createObjectURL(this.editParam.bgThumbnail.src)
    },
    previewImg (cover) {
      this.imgUrl = cover
      this.imgVisible = true
    },
    changeShowNav (event, id, title, index) {
      var tip = event === true ? '确定显示专题“' + title + '”的导航菜单？' : '确定隐藏专题“' + title + '”的导航菜单？'
      var param = {
        id: id,
        show_nav: event === true ? 1 : 0
      }
      this.changeStatus(param, tip, () => {
        this.listData.list[index].show_nav = !event
      })
    },
    changeStatus (param, tip, fn) {
      this.$confirm(tip)
        .then(_ => {
          api.GET(helper.getManageApiUrl(apiConfig.switchUpdateSubject), param, res => {
            this.$notify({
              title: '成功',
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.getlist()
          }, res => {
            this.$notify({
              title: '失败',
              message: res.msg,
              type: 'error',
              duration: 1500
            })
          })
        }).catch(_ => {
          typeof fn === 'function' && fn()
        })
    }
  }
}
</script>
<style>
 .search-box {
  font-size: 0;
  padding-bottom:10px;
 }
 .search-box .user-input{
   width: 197px;
   margin: 3px 2px;
 }

 .input-group-box .el-upload--picture-card {
   position: relative;
   width: 100px;
   height: 100px;
 }
 .input-group-box img.cover {
   width: 95px;
   height: 71px;
   display: block;
   margin-bottom: 10px;
 }
 .input-group-box .el-upload--picture-card i {
   position: absolute;
   top: 35px;
   left: 35px;
 }
 .el-upload--picture-card .zi_upload_input {
   position: absolute;
   display: inline-block;
   width: 100%;
   height: 100%;
   opacity: 0;
   top: 0;
   left: 0;
   cursor: pointer;
 }
 .cover-box .p-tip {
   display: inline-block;
   color: red;
   font-size: 12px;
   margin-top: 80px;
   margin-left: 10px;
 }
 .art-cover {
   width: 95px;
   height: 70px;
 }
</style>

